﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Marbles.Core.Connectors;
using System.Data;

namespace Marbles.Core.Content.Assets
{
    public class AssetCollection : ICollection<Asset>
    {

        public int Count
        {
            get { return _items.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public Asset this[int index] { get { return _items[index]; } set { _items[index] = value; } }

        private List<Asset> _items = null;

        public AssetCollection()
        {
            _items = new List<Asset>();
        }

        public AssetCollection(string SiteId)
            : this()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("SELECT * FROM assets WHERE siteid = @siteid ORDER BY filename ASC");
            cmd.AddParameter("@siteid", SiteId);
            foreach (DataRow dr in dbc.FetchDataTable(cmd).Rows)
            {
                _items.Add(new Asset(dr));
            }
        }

        public AssetCollection(string SiteId, string ContentType)
            : this()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("SELECT * FROM assets WHERE siteid = @siteid AND contenttype LIKE @contenttype ORDER BY filename ASC");
            cmd.AddParameter("@siteid", SiteId);
            cmd.AddParameter("@contenttype", ContentType.Replace('*', '%'));

            foreach (DataRow dr in dbc.FetchDataTable(cmd).Rows)
            {
                _items.Add(new Asset(dr));
            }
        }

        public void Add(Asset item)
        {
            _items.Add(item);
        }

        public void Clear()
        {
            _items.Clear();
        }

        public bool Contains(Asset item)
        {
            return _items.Contains(item);
        }

        public void CopyTo(Asset[] array, int arrayIndex)
        {
            _items.CopyTo(array, arrayIndex);
        }

        public bool Remove(Asset item)
        {
            return _items.Remove(item);
        }

        public IEnumerator<Asset> GetEnumerator()
        {
            return _items.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _items.GetEnumerator();
        }
    }
}